package com.example.testfocus;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class MainActivity extends AppCompatActivity {

    private int mYear;
    private int mMonth;
    private int mDay;
    private int mWeek;
    private static Date date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Date date = new Date();
        long time1 = date.getTime();


        Log.i("eyesee1",time1+"");

        long getmillis = getmillis("2017-3-30 14:00:00");
        Log.i("eyesee3",getmillis+"");

        String time = getFormatedDateTime("yy-MM-dd HH:mm:ss", getmillis);
        Log.i("eyesee2",time);

        int week = millis2week(getmillis);
        Log.i("eyesee4",week+"");

    }

    private void getWeek() {
        Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT+8"));
        calendar.setTimeInMillis(Calendar.getInstance().getTimeInMillis());
        mYear = calendar.get(Calendar.YEAR);
        mMonth = calendar.get(Calendar.MONTH) + 1;
        mDay = calendar.get(Calendar.DATE);
        mWeek = calendar.get(Calendar.WEEK_OF_MONTH);
        //日期格式
        String time = mYear + "月" + mMonth + "月" + mDay + "日";

        //格式化日期
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar instance = Calendar.getInstance();
        try {
            instance.setTime(format.parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int week = instance.get(Calendar.DAY_OF_WEEK);
    }

    private void getcurTime() {
        //指定东8时区
        Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT+8"));
        calendar.setTimeInMillis(Calendar.getInstance().getTimeInMillis());
        long millis = calendar.getTimeInMillis();
        Date date = new Date(millis);
        long time = date.getTime();
        int hours = date.getHours();
        int week = calendar.get(Calendar.DAY_OF_WEEK);
    }

    private int millis2week(long millis){
        SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        Date date = new Date(millis);
        String year = sdf.format(date);
        Calendar instance = Calendar.getInstance();
        try {
            instance.setTime(sdf.parse(year));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int week = instance.get(Calendar.DAY_OF_WEEK)-1;
        return week;
    }

    public static String getFormatedDateTime(String pattern, long dateTime) {
        SimpleDateFormat sDateFormat = new SimpleDateFormat(pattern);
        Calendar instance = Calendar.getInstance();
        return sDateFormat.format(new Date(dateTime + 0));
    }

    //时间转换成毫秒
    public static long getmillis(String t){
        SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        try {
            date = sdf.parse(t);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long time = date.getTime();
        return time;
    }

    //时间转换成毫秒   "yy-MM-dd HH:mm:ss"
    public static long getmilli(String t){
        SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        Date parse = null;
        try {
            parse = sdf.parse(t);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long time = parse.getTime();
        return time;
    }

    //毫秒值格式化成年月日时分秒格式   "yy-MM-dd HH:mm:ss", millis
    public static String getFormatedDateTim(String pattern, long dateTime) {
        SimpleDateFormat sDateFormat = new SimpleDateFormat(pattern);
        Calendar instance = Calendar.getInstance();
        return sDateFormat.format(new Date(dateTime + 0));
    }

    //毫秒值转换成星期
    private int millis2weeks(long millis){
        SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        Date date = new Date(millis);
        String year = sdf.format(date);
        Calendar instance = Calendar.getInstance();
        try {
            instance.setTime(sdf.parse(year));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int week = instance.get(Calendar.DAY_OF_WEEK)-1;
        return week;
    }

    //毫秒值转换成时分格式
    public String gettime(long m){
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String time = sdf.format(m);
        return time;
    }
}
